package com.linuxgt.android.dbapp.service;

import android.support.test.InstrumentationRegistry;
import android.util.Log;

import com.linuxgt.android.dbapp.domain.User;

import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.*;

/**
 * Created by liuteng on 2017/5/17.
 */
public class UserServiceTest {

    private UserService userService;

    @Test
    public void payment() throws Exception {
        userService.payment(1, 2, 10);
        User from = userService.query(1);
        User to = userService.query(2);
        assertEquals(0, from.getAmount() + to.getAmount());
    }

    @Before
    public void init() throws Exception {
        userService = new UserService(InstrumentationRegistry.getTargetContext());
    }

    @Test
    public void save() throws Exception {
        User user = new User("test", 99);
        userService.save(user);
    }

    @Test
    public void delete() throws Exception {
        User user = userService.query(99);
        if (null == user) {
            userService.save(new User(99, "test", 99));
            user = userService.query(99);
        } else {
            userService.delete(99);
        }
        userService.save(user);
        userService.delete(99);
    }

    @Test
    public void query() throws Exception {
        User user = userService.query(99);
        if (null == user) {
            userService.save(new User(99, "test", 99));
            user = userService.query(99);
        }
        Log.i(getClass().getSimpleName(), user.toString());
    }

    @Test
    public void getPageData() throws Exception {
        List<User> users = userService.getPageData(1, 10);
        for (User user : users) {
            Log.i(getClass().getSimpleName(), user.toString());
        }
    }

    @Test
    public void update() throws Exception {
        User user = new User(1, "liuteng", 28);
        userService.update(user);
        Log.i(getClass().getSimpleName(), user.toString());
    }

    @Test
    public void getCount() throws Exception {
        Log.i(getClass().getSimpleName(), String.valueOf(userService.getCount()));
    }

}